package com.cgsd.heyu.ui.home.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.library.image.GlideUtil;
import com.android.library.utils.BeanUtils;
import com.android.library.utils.SystemUtil;
import com.cgsd.heyu.R;
import com.cgsd.heyu.common.AppMobclickAgent;
import com.cgsd.heyu.common.AvatarManger;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.common.UserModeConfig;
import com.cgsd.heyu.entity.BannerEntity;
import com.cgsd.heyu.entity.EventMessage;
import com.cgsd.heyu.entity.FlowerInfoEntity;
import com.cgsd.heyu.http.ErrorMessage;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.Integral.IntegralDetailActivity;
import com.cgsd.heyu.ui.Integral.IntegralShopActivity;
import com.cgsd.heyu.ui.common.TabActivity;
import com.cgsd.heyu.ui.info.CashOutActivity;
import com.cgsd.heyu.ui.info.InformationActivity;
import com.cgsd.heyu.ui.info.MyTeamActivity;
import com.cgsd.heyu.ui.info.SettingActivity;
import com.cgsd.heyu.ui.info.ShareSpreadActivity;
import com.cgsd.heyu.ui.info.VipActivity;
import com.cgsd.heyu.ui.task.PublishTaskActivity;
import com.cgsd.heyu.ui.webview.CommonH5Activity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 说明
 * CreateData: 2020/11/3
 */
public class CenterFragment extends BaseFragment implements View.OnClickListener, HomeContract.CenterView {
    private HomeContract.Presenter mPresenter;
    private ImageView ivAvatar;
    private TextView tvNickName;
    private TextView tvDes;
    private TextView tvHarvestNum;
    private ImageView ivBanner;
    private List<BannerEntity> bannerEntities = new ArrayList<>();
    private LinearLayout layoutCashOut;
    private FlowerInfoEntity flowerInfoEntity;
    private TextView tvLevel;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_center;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        ivAvatar = findViewById(R.id.iv_avatar);
        tvNickName = findViewById(R.id.tv_nickName);
        tvDes = findViewById(R.id.tv_des);
        tvHarvestNum = findViewById(R.id.tv_harvest_num);
        ivBanner = findViewById(R.id.iv_banner);
        layoutCashOut = findViewById(R.id.layout_cash_out);
        tvLevel = findViewById(R.id.tv_level);

        ivBanner.setOnClickListener(this);
        findViewById(R.id.layout_cash_out).setOnClickListener(this);
        findViewById(R.id.layout_settting).setOnClickListener(this);
        findViewById(R.id.layout_info).setOnClickListener(this);
        findViewById(R.id.layout_free_purchase).setOnClickListener(this);
        findViewById(R.id.layout_my_team).setOnClickListener(this);
        findViewById(R.id.iv_copy).setOnClickListener(this);
        findViewById(R.id.layout_take_task).setOnClickListener(this);
        findViewById(R.id.layout_publish_task).setOnClickListener(this);
        findViewById(R.id.layout_vip).setOnClickListener(this);
        findViewById(R.id.layout_share_spread).setOnClickListener(this);
        findViewById(R.id.layout_my_internal).setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        setUserData();
        mPresenter.getBannerType(BannerEntity.TYPE_CENTER);
//        if (CommonConfig.getInstance().isShowCashOut) {
//            layoutCashOut.setVisibility(View.VISIBLE);
//        } else {
//            layoutCashOut.setVisibility(View.GONE);
//        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getFlowerInfo(UserModeConfig.getInstance().userId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_cash_out://积分提现
                CashOutActivity.actionStart((Activity) mContext, flowerInfoEntity);
                break;
            case R.id.layout_settting:
                SettingActivity.actionStart((Activity) mContext);
                break;
            case R.id.layout_info://会员信息
                InformationActivity.actionStart((Activity) mContext);
                break;
            case R.id.iv_banner:
                AppMobclickAgent.onEvent(AppMobclickAgent.WO_BANNER1);
                CommonH5Activity.actionStart((Activity) mContext, bannerEntities.get(0).bannerHrefUrl, null);
                break;
            case R.id.layout_free_purchase:
                IntegralShopActivity.actionStart((Activity) mContext);
                break;
            case R.id.layout_my_team://我的团队
                MyTeamActivity.actionStart((Activity) mContext);
                break;
            case R.id.iv_copy:
                SystemUtil.copyStr(mContext, UserModeConfig.getInstance().memberNo);
                showToast("复制成功");
                break;
            case R.id.layout_take_task://悬赏
                switchTask();
//                startActivity(PayActivity.getIntent(mContext,"391",20));
                break;
            case R.id.layout_publish_task:
                startActivity(PublishTaskActivity.getIntent(mContext));
                break;
            case R.id.layout_vip:
                VipActivity.actionStart((Activity) mContext);
                break;
            case R.id.layout_share_spread:
                ShareSpreadActivity.actionStart((Activity) mContext);
                break;
            case R.id.layout_my_internal://我的积分
                IntegralDetailActivity.actionStart((Activity) mContext, flowerInfoEntity.usableAmount);
                break;
            default:
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventMessage event) {
        if (event.getCode() == Constants.EventCode.USER_REFRESH) {
            setUserData();
        }
    }

    /**
     * 设置用户数据
     */
    @SuppressLint("SetTextI18n")
    private void setUserData() {
        AvatarManger.setAvatar(mContext, UserModeConfig.getInstance().headImgUrl, ivAvatar);
        tvNickName.setText(UserModeConfig.getInstance().nickName);
        tvDes.setText("邀请码： " + UserModeConfig.getInstance().memberNo);
        switch (UserModeConfig.getInstance().vipLevel) {
            case UserModeConfig.VIP_LEVEL1:
                tvLevel.setText("普通会员");
                break;
            case UserModeConfig.VIP_LEVEL2:
                tvLevel.setText("VIP用户");
                break;
            case UserModeConfig.VIP_LEVEL3:
                tvLevel.setText("合伙人");
                break;
            case UserModeConfig.VIP_LEVEL4:
                tvLevel.setText("董事");
                break;
        }
    }

    @Override
    public void setFlowerInfo(FlowerInfoEntity entity) {
        if (entity == null) {
            return;
        }
        flowerInfoEntity = entity;
        tvHarvestNum.setText(entity.usableAmount);
    }

    @Override
    public void setBanner(List<BannerEntity> entities) {
        if (BeanUtils.isEmpty(entities)) {
            return;
        }
        if (BannerEntity.STATUS_ON.equals(entities.get(0).bannerStatus)) {
            bannerEntities.addAll(entities);
//            ivBanner.setVisibility(View.VISIBLE);
            GlideUtil.loadRemoteImage(mContext, entities.get(0).bannerImageUrl, ivBanner);
        }
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        mPresenter = BeanUtils.checkNotNull(presenter);
    }

    @Override
    public void setErrorMessage(ErrorMessage error) {
        handleErrorAction(error);
    }

    /**
     * switch task
     */
    private void switchTask() {
        if (getActivity() instanceof TabActivity) {
            TabActivity activity = (TabActivity) getActivity();
            activity.onTabClick(2,0);
        }
    }
}
